package com.example.weather_news.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weather_news.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 逝 on 2017/06/16.
 */

public class Adapter_Hours extends RecyclerView.Adapter<Adapter_Hours.ViewHolder> {
    private Context context;
    private JSONArray arrays = new JSONArray();

    public Adapter_Hours(Context context) {
        this.context = context;
    }

    public void setData(JSONArray arrays){
        this.arrays=arrays;
        this.notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.hours24_item, null);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        try {
            JSONObject jo=arrays.getJSONObject(position);
            String time=jo.getString("time");
            String weather=jo.getString("weather");
            String temp=jo.getString("temp");
            int img=jo.getInt("img");
            switch (img){
                case 0:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.qing);
                    break;
                case 1:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.duoyun_bai);
                    break;
                case 2:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.yin);
                    break;
                case 3:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.zhenyu);
                    break;
                case 4:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.leizhenyu);
                    break;
                case 5:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.ic_launcher_round);
                    break;
                case 6:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.ic_launcher_round);
                    break;
                case 7:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.xiaoyu);
                    break;
                case 8:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.zhongyu);
                    break;
                case 9:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.dayu);
                    break;
                case 10:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.baoyu);
                    break;
                case 22:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.zhongyu);
                    break;
                default:
                    holder.ivWeatherImg.setBackgroundResource(R.mipmap.ic_launcher_round);
                    break;
            }
            holder.tvHoursTime.setText(time);
            holder.tvHoursType.setText(weather);
            holder.tvHoursTemp.setText(temp+"°");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return arrays.length();
    }

    protected class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvHoursTime;
        private ImageView ivWeatherImg;
        private TextView tvHoursType;
        private TextView tvHoursTemp;

        public ViewHolder(View view) {
            super(view);
            tvHoursTime = (TextView) view.findViewById(R.id.tv_hours_time);
            ivWeatherImg = (ImageView) view.findViewById(R.id.iv_weatherImg);
            tvHoursType = (TextView) view.findViewById(R.id.tv_hours_type);
            tvHoursTemp = (TextView) view.findViewById(R.id.tv_hours_temp);
        }
    }
}
